<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="query"
        ref="queryForm"
        v-show="showSearch"
        :inline="true"
        label-width="63px"
      >
        <el-form-item label="报表名称" prop="name">
          <common-input v-model="query.name" clearable @enter="handleQuery" />
        </el-form-item>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->

    <!-- 按钮部门start -->
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="addView = true"
          v-hasPermi="['system:reportconfig:add']"
          >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-upload2" @click="exportData(selectionIds)"
                         v-hasPermi="['system:reportconfig:export']">导出
              </el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="del(selectionIds)"
          v-hasPermi="['system:reportconfig:delete']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮部门end -->

    <!--列表 start-->
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="报表名称" align="left" prop="name">
        <template slot-scope="{ row }">{{ row.name }}</template>
      </el-table-column>

      <el-table-column
        label="描述"
        align="left"
        prop="remark"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column label="操作" align="left" width="125">
        <template slot-scope="{ row }">
          <el-button type="text" @click="detail(row)">详情</el-button>
          <el-button
            type="text"
            @click="edit(row)"
            v-hasPermi="['system:reportconfig:edit']"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="del(row.id)"
            v-hasPermi="['system:reportconfig:delete']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />
    <!-- 列表 end -->

    <addView
      ref="addView"
      v-if="addView"
      @back="addView = false"
      @refresh="getList"
    />
    <detailView ref="detailView" v-if="detailView" @back="detailView = false" />
  </common-list>
</template>

<script>
import addView from "./components/addView.vue";
import detailView from "./components/detailView.vue";
import {
  report_list,
  report_remove,
  report_getById,
  report_get,
  report_export,
} from "@/api/management/echart/report-config";
import BaseList from "@/mixins/BaseList";

export default {
  mixins: [BaseList],
  components: { addView, detailView },
  props: {},
  data() {
    return {
      addView: false,
      detailView: false,
      showSearch: true, // 查询部分显示
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      selectionIds: "", // 列表选中数据id
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      query: {
        name: null,
        pageNum: 1,
        pageSize: 10,
      }, // 查询条件
    };
  },
  watch: {},
  computed: {},
  methods: {
    /**
     * 导出
     */
    exportData() {
      report_export({}).then((res) => {
        // console.log(res);
      });
    },
    edit(row) {
      report_getById({ id: row.id }).then((res) => {
        this.addView = true;
        if (res.data) {
          this.$nextTick(() => {
            this.$refs.addView.edit(res.data);
          });
        }
      });
    },
    detail(row) {
      report_getById({ id: row.id }).then((res) => {
        this.detailView = true;
        if (res.data) {
          this.$nextTick(() => {
            this.$refs.detailView.show(res.data);
          });
        }
      });
    },
    handleSelectionChange(selection) {
      this.selectionIds = selection.map((item) => item.id).join(",");
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    getList() {
      this.addView = false;
      this.loading = true;
      report_list({ ...this.query })
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
    },
    /**
     * 删除
     */
    del(ids) {
      let message = (
        <p>
          <p>是否确认删除数据项?</p>
          <p style="font-size:12px">若删除,关联的大屏配置数据也一并被删除!</p>
        </p>
      );
      this.$GlobalDeleteConfirm(message)
        .then(() => {
          report_remove({ ids: ids }).then((res) => {
            if (res && res.code === 200) {
              this.getList();
              this.$GlobalSuccessMessage("删除成功");
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg).then();
            }
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
